<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        #login-all {
            border: 1px solid rosybrown;
            height: 300px;
            width: 500px;
            margin: 200px auto;
            padding: 100px 120px 100px 100px;
            box-shadow: 15px 15px 15px 15px #888888;
        }
    </style>
</head>
<body>
<div id="login-all">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名 : </label>
            <div class="layui-input-inline">
                <input type="text" name="username" lay-verify="username" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密 码:</label>
            <div class="layui-input-inline">
                <input type="password" name="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off"
                       class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
        </div>
       <!-- <div class="layui-form-item">
            <label class="layui-form-label">验证码 : </label>
            <div class="layui-input-inline">
                <input type="text" name="capchar" id="capchar" lay-verify="capchar" autocomplete="off"
                       placeholder="请输入验证码"
                       class="layui-input">
            </div>
            <div>
                <img src="/captcha" id="cap" alt="" style="width: 85px;height: 35px"/><label id="checkMsg"></label>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">滑动验证</label>
            <div class="layui-input-block">
                <div id="slider"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" id="login_btn" class="layui-btn" lay-submit="" lay-filter="login_btn">立即提交
                </button>
                <button class="layui-btn" type="button" id="reset">重置滑块</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="./layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;
        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length < 3) {
                    return '标题至少得3个字符啊';
                }
            },
            pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
        });
     /*   $("#cap").click(function () {
            var path = $(this).attr("src") + "?" + new Date();
            $(this).attr("src", path);
        });*/
        var slider = sliderVerify.render({
            elem: '#slider',
            onOk: function(){//当验证通过回调
                layer.msg("滑块验证通过");
            }
        })
        $('#reset').on('click',function(){
            slider.reset();
        });
        //监听提交
        form.on('submit(login_btn)', function (data) {

            $.ajax({
                url: "/user/login",
                data: JSON.stringify(data.field),
                contentType: "application/json",
                type: "POST",
                success: function (result) {
                    // 登录成功进行页面跳转
                    if (result.code === "1") {
                        window.location = "/index.html";
                    } else {
                        // 登录失败给出提示信息
                        layer.alert(result.message, {
                            title: '提示信息'
                        });
                    }
                }
            });
            return false;
            // 阻止表单的默认提交行为
        });

    });

</script>

</body>
</html>